﻿namespace MDXStudio.QueryEditor
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    internal class TextBoxAPIHelper
    {
        private const double anInch = 14.4;

        private TextBoxAPIHelper()
        {
        }

        internal static int CharFromPos(TextBoxBase txt, Point pt)
        {
            int num = (pt.X & 0xffff) + ((pt.Y & 0xffff) << 0x10);
            int num2 = MDXStudio.QueryEditor.NativeMethods.SendMessageInt(txt.Handle, 0xd7, IntPtr.Zero, new IntPtr(num)).ToInt32();
            int num3 = (num2 & 0xffff) >> 0x10;
            int num4 = num2 & 0xffff;
            return (MDXStudio.QueryEditor.NativeMethods.SendMessageInt(txt.Handle, 0xbb, new IntPtr(num3), IntPtr.Zero).ToInt32() + num4);
        }

        internal static int GetBaselineOffsetAtCharIndex(TextBoxBase tb, int index)
        {
            MDXStudio.QueryEditor.NativeMethods.CHARRANGE charrange;
            MDXStudio.QueryEditor.NativeMethods.RECT rect;
            MDXStudio.QueryEditor.NativeMethods.RECT rect2;
            MDXStudio.QueryEditor.NativeMethods.FORMATRANGE formatrange;
            RichTextBox box = tb as RichTextBox;
            if (box == null)
            {
                return tb.Font.Height;
            }
            int lineFromCharIndex = box.GetLineFromCharIndex(index);
            int num2 = MDXStudio.QueryEditor.NativeMethods.SendMessageInt(box.Handle, 0xbb, new IntPtr(lineFromCharIndex), IntPtr.Zero).ToInt32();
            int num3 = MDXStudio.QueryEditor.NativeMethods.SendMessageInt(box.Handle, 0xc1, new IntPtr(lineFromCharIndex), IntPtr.Zero).ToInt32();
            charrange.cpMin = num2;
            charrange.cpMax = num2 + num3;
            rect.Top = 0;
            rect.Bottom = 14;
            rect.Left = 0;
            rect.Right = 0x989680;
            rect2.Top = 0;
            rect2.Bottom = 14;
            rect2.Left = 0;
            rect2.Right = 0x989680;
            Graphics graphics = Graphics.FromHwnd(box.Handle);
            IntPtr hdc = graphics.GetHdc();
            formatrange.chrg = charrange;
            formatrange.hdc = hdc;
            formatrange.hdcTarget = hdc;
            formatrange.rc = rect;
            formatrange.rcPage = rect2;
            MDXStudio.QueryEditor.NativeMethods.SendMessage(box.Handle, 0x439, IntPtr.Zero, ref formatrange).ToInt32();
            graphics.ReleaseHdc(hdc);
            graphics.Dispose();
            return (int) (((double) (formatrange.rc.Bottom - formatrange.rc.Top)) / 14.4);
        }

        internal static int GetFirstVisibleLine(TextBoxBase txt)
        {
            return MDXStudio.QueryEditor.NativeMethods.SendMessageInt(txt.Handle, 0xce, IntPtr.Zero, IntPtr.Zero).ToInt32();
        }

        internal static int GetLineIndex(TextBoxBase txt, int line)
        {
            return MDXStudio.QueryEditor.NativeMethods.SendMessageInt(txt.Handle, 0xbb, new IntPtr(line), IntPtr.Zero).ToInt32();
        }

        internal static int GetTextWidthAtCharIndex(TextBoxBase tb, int index, int length)
        {
            MDXStudio.QueryEditor.NativeMethods.CHARRANGE charrange;
            MDXStudio.QueryEditor.NativeMethods.RECT rect;
            MDXStudio.QueryEditor.NativeMethods.RECT rect2;
            MDXStudio.QueryEditor.NativeMethods.FORMATRANGE formatrange;
            RichTextBox box = tb as RichTextBox;
            if (box == null)
            {
                return tb.Font.Height;
            }
            charrange.cpMin = index;
            charrange.cpMax = index + length;
            rect.Top = 0;
            rect.Bottom = 14;
            rect.Left = 0;
            rect.Right = (int) (box.ClientSize.Width * 14.4);
            rect2.Top = 0;
            rect2.Bottom = 14;
            rect2.Left = 0;
            rect2.Right = (int) (box.ClientSize.Width * 14.4);
            Graphics graphics = Graphics.FromHwnd(box.Handle);
            IntPtr hdc = graphics.GetHdc();
            formatrange.chrg = charrange;
            formatrange.hdc = hdc;
            formatrange.hdcTarget = hdc;
            formatrange.rc = rect;
            formatrange.rcPage = rect2;
            MDXStudio.QueryEditor.NativeMethods.SendMessage(box.Handle, 0x439, IntPtr.Zero, ref formatrange);
            graphics.ReleaseHdc(hdc);
            graphics.Dispose();
            return (int) (((double) (formatrange.rc.Right - formatrange.rc.Left)) / 14.4);
        }

        internal static Point PosFromChar(TextBoxBase txt, int charIndex)
        {
            return new Point(MDXStudio.QueryEditor.NativeMethods.SendMessageInt(txt.Handle, 0xd6, new IntPtr(charIndex), IntPtr.Zero).ToInt32());
        }

        internal static void ScrollLineDown(TextBoxBase txt)
        {
            MDXStudio.QueryEditor.NativeMethods.SendMessageInt(txt.Handle, 0xb5, (IntPtr) 1, IntPtr.Zero).ToInt32();
        }

        internal static void ScrollLineUp(TextBoxBase txt)
        {
            MDXStudio.QueryEditor.NativeMethods.SendMessageInt(txt.Handle, 0xb5, IntPtr.Zero, IntPtr.Zero).ToInt32();
        }

        internal static void ShowCaret(TextBox txt)
        {
            bool flag = false;
            for (int i = 0; !flag && (i < 10); i++)
            {
                flag = MDXStudio.QueryEditor.NativeMethods.ShowCaretAPI(txt.Handle);
            }
        }
    }
}

